(터미널에서)
pip3 install gcloud
gcloud auth login
In [1]:
from gcloud import bigquery as bq
import uuid
import os
In [2]:
client = bq.Client()
In [4]:
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "/Path/to/service_key.json"
In [3]:
client = bq.Client()
In [ ]:
# client의 project명 print
print(client.project)
In [4]:
# dataset 설정
dataset = client.dataset("bigquery-public-data:samples")
In [5]:
query_job = client.run_async_query(str(uuid.uuid4()), """
#standardSQL
SELECT corpus AS title, COUNT(*) AS unique_words
FROM `publicdata.samples.shakespeare`
GROUP BY title
ORDER BY unique_words DESC
LIMIT 10""")
In [8]:
# query 실행
query_job.begin()
In [9]:
destination_table = query_job.destination
destination_table.reload()
for row in destination_table.fetch_data():
print(row)
In [10]:
query_string = '''
#standardSQL
SELECT corpus AS title, COUNT(*) AS unique_words
FROM `publicdata.samples.shakespeare`
GROUP BY title
ORDER BY unique_words DESC
LIMIT 10
'''
In [11]:
query = client.run_sync_query(query_string)
query.timeout_ms = 5 * 60 * 1000
In [12]:
query.run()
In [13]:
data = query.fetch_data()[0]
In [14]:
data
Out[14]:
In [6]:
from pandas.io import gbq
In [8]:
query = '''
#standardSQL
SELECT corpus AS title, COUNT(*) AS unique_words
FROM `publicdata.samples.shakespeare`
GROUP BY title
ORDER BY unique_words DESC
LIMIT 10
'''
In [13]:
gbq.read_gbq(query = query, project_id = 'project_id', dialect='standard')
Out[13]: